using System;
using System.Collections.Generic;

namespace Battle.TimelineLog
{
    public class HashList<T> where T : ISysMainKey
    {
        protected Dictionary<string, T> m_dic;
        protected List<T> m_list;

        public HashList()
        {
            m_dic = new Dictionary<string, T>();
            m_list = new List<T>();
        }
        /// <summary>
        /// 使用数值初始化
        /// </summary>
        /// <param name="arr"></param>
        public void Init(T[] arr)
        {
            if (arr == null) return;
            for (int i = 0, n = arr.Length; i < n; i++)
            {
                AddData(arr[i]);
            }
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetData(object key)
        {
            return GetData(key.ToString());
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetData(string key)
        {
            T result = default(T);
            m_dic.TryGetValue(key, out result);
            return result;
        }
        /// <summary>
        /// 获取List
        /// </summary>
        /// <returns></returns>
        public List<T> GetList()
        {
            return m_list;
        }
        /// <summary>
        /// 增加数据返回false为覆盖，返回true为增加ok
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool AddData(T data)
        {
            string key = data.MainKey;
            var temp = GetData(key);
            if (temp == null)
            {
                m_list.Add(data);
                m_dic.Add(key, data);
                return true;
            }
            else
            {
                m_dic.Remove(key);
                m_dic.Add(key, data);
                return false;
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public T RemoveData(T data)
        {
            string key = data.MainKey;
            return RemoveData(key);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T RemoveData(string key)
        {
            var temp = GetData(key);
            if (temp != null)
            {
                m_dic.Remove(key);
                m_list.Remove(temp);
                return temp;
            }
            return default;
        }
        /// <summary>
        /// 遍历所有字段
        /// </summary>
        /// <param name="compareFunc"></param>
        public void ForEach(Action<T> compareFunc)
        {
            m_list.ForEach(compareFunc);
        }

        /// <summary>
        /// 查找对象
        /// </summary>
        /// <param name="compareFunc"></param>
        /// <returns></returns>
        public T Find(Predicate<T> compareFunc)
        {
            return m_list.Find(compareFunc);
        }
        /// <summary>
        /// 查找所有符合要求的对象
        /// </summary>
        /// <param name="compareFunc"></param>
        /// <returns></returns>
        public List<T> FindAll(Predicate<T> compareFunc)
        {
            return m_list.FindAll(compareFunc);
        }
        /// <summary>
        /// 获得数据条目数
        /// </summary>
        /// <value></value>
        public int Count
        {
            get { return m_list.Count; }
        }

        /// <summary>
        /// 销毁
        /// </summary>
        public void Destroy()
        {
            m_list.Clear();
            m_dic.Clear();
            m_list = null;
            m_dic = null;
        }
    }
}